<?php

namespace App\Http\Controllers\Admin\OrderManager;

use App\Enums\ExportEnum;
use App\Enums\RedisKeyEnum;
use App\Jobs\QiPao\ExcelJob;
use App\Services\Tools\Export;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Services\Tools\ExportService;
use Illuminate\Http\Request;

//引用http请求类
use App\Response\Response;

class CorporateAccountDrawController extends BaseController
{

    /**
     * @apiVersion 0.1.0
     * @apiName 订单管理-对公账号提现-批量审核
     * @apiGroup 订单管理
     * @apiParam {number} ids                         记录id，逗号分隔  必传
     * @apiParam {number} type                        1 通过 2 拒绝  必传
     * @apiSuccessExample 成功响应
     *      {"code":200,"data":true}
     * @apiError 10001 参数校验失败
     * @apiErrorExample 错误响应
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     */
    public function check(Request $request)
    {
        try {
            $this->validate($request, [
                'ids'       => 'required|string',
                'type'      => 'required|numeric|integer|in:1,2',
                'fail_reason'    => 'string|max:255',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $params = $request->input();

        $res = app('repo_user_withdraw');

        $idArr = explode(",", $params['ids']);
        $type = $params['type'];
        $failReason = $params['fail_reason'] ?? '';

        $rows = $res->batchBankCheck($idArr, $type, $failReason);
        if (!$rows) {
            throw new BasicException(10001, "审核失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @apiVersion 0.1.0
     * @apiName 订单管理-对公账号提现-备注
     * @apiGroup 订单管理
     * @apiParam {int} id
     * @apiParam {string} remark                       备注 必传
     * @apiSuccessExample 成功响应
     *      {"code":200,"data":true}
     * @apiError 10001 参数校验失败
     * @apiErrorExample 错误响应
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     */
    public function remark(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'id' => 'required|integer',
                'remark' => 'required|string',
            ]);
        if ($v->fails()) {
            throw new BasicException(10001, getValidateMessage($v->messages()));
        }
        $id = $request->input('id');

        $rows = app('repo_user_withdraw')->setRemark($id, $request->input());
        if (!$rows) {
            throw new BasicException(10001, "备注失败！");
        }

        return Response::sendData(true);
    }


    /**
     * @apiVersion 0.1.0
     * @apiName 订单管理-对公账号提现-列表
     * @apiGroup 订单管理
     * @apiParam {number} type 来源  1从app提现2从h5提现 可选
     * @apiParam {string} remark 备注 可选
     * @apiParam {string} bank_num 账号 可选
     * @apiParam {number} bank_type 账号 1.对公账号  2.微信 3.对公账号 可选
     * @apiParam {string} real_name 姓名 可选
     * @apiParam {number} user_code 用户编号 可选
     * @apiParam {number} status 状态 1.待审核 2.通过 3.拒绝 可选
     * @apiParam {string} start_time 开始时间 可选
     * @apiParam {string} end_time  结束时间 可选
     * @apiParam {string} mobile  手机号 可选
     * @apiParam {number} page  页码 可选
     * @apiParam {number} perPage  分页容量 可选
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "list": [
     * {
     * "id(序号)": 31398,
     * "user_code(用户编号)": 617040,
     * "money(提现金额)": "89900.00",
     * "cardholder(姓名)": "周亮",
     * "bank_num(对公账号账号)": "zl880915zl@163.com",
     * "add_time(申请时间)": "2020-11-02 22:26:20",
     * "audit_time(审批时间)": "2020-11-12 16:09:16",
     * "type(来源)": 2,
     * "status(状态)": 3,
     * "remark(备注)": "123",
     * "mobile(手机号)": "18658172093",
     * "origin_mobile(预留手机号)": "18658172093",
     * "bank_name(开户行)": "对公账号",
     * "bank_zhi(支行)": "",
     * "card_number(身份证)": "",
     * "bank_type(类别 1.对公账号  2.微信 3.对公账号)": 1,
     * "gold(提现金币)": 8990000
     * }
     * ],
     * "meta": {
     * "page": 1,
     * "limit": 10,
     * "next": null,
     * "total": 1
     * }
     * }
     * }
     */
    public function list(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'user_code'           => 'filled|numeric|integer',
                'mobile'              => 'filled|string',                       //手机号
                'bank_num'            => 'filled|string',                       //用户卡号
                'platform_status'     => 'filled|numeric|integer|in:1,2,3',
                'status'              => 'filled|numeric|integer|in:1,2,3',
                'start_time'          => 'filled|string|date_format:Y-m-d',
                'end_time'            => 'filled|string|date_format:Y-m-d',
                'audit_start_time'    => 'filled|string|date_format:Y-m-d',
                'audit_end_time'      => 'filled|string|date_format:Y-m-d',
                'operate_start_time'  => 'filled|string|date_format:Y-m-d',
                'operate_end_time'    => 'filled|string|date_format:Y-m-d',
                'page'                => 'filled|numeric',
                'page_size'             => 'filled|numeric',
            ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }

        $params = $request->input();

        $list = app('repo_user_withdraw')->getCorporateWithDrawQueryList($params);

        return Response::sendStatList($list);
    }

    /**
     * @apiVersion 0.1.0
     * @apiName 对公账号提现-导出数据
     * @apiGroup 订单管理
     * @apiParam {number} type 来源  1从app提现2从h5提现 可选
     * @apiParam {string} remark 备注 可选
     * @apiParam {string} bank_num 账号 可选
     * @apiParam {string} real_name 姓名 可选
     * @apiParam {number} user_code 用户编号 可选
     * @apiParam {number} status 状态 1.待审核 2.通过 3.拒绝 可选
     * @apiParam {string} start_time 开始时间 可选
     * @apiParam {string} end_time  结束时间 可选
     * @apiParam {string} mobile  手机号 可选
     * @apiParam {number} bank_type 账号 1.对公账号  2.微信 3.对公账号 可选
     * @apiParam {number} page  页码 可选
     * @apiParam {number} perPage  分页容量 可选
     * @apiSuccessExample 成功响应
     *  {
     *   "code": 200,
     *   "data": {
     *       "download_url": "asdasdasd.com"
     *       }
     *  }
     */
    public function export(Request $request)
    {
        //参数校验
        try {
            $this->validate($request, [
                'user_code'           => 'filled|numeric|integer',
                'mobile'              => 'filled|string',                       //手机号
                'bank_num'            => 'filled|string',                       //用户卡号
                'platform_status'     => 'filled|numeric|integer|in:1,2,3',
                'status'              => 'filled|numeric|integer|in:1,2,3',
                'start_time'          => 'filled|string|date_format:Y-m-d',
                'end_time'            => 'filled|string|date_format:Y-m-d',
                'page'                => 'filled|numeric',
                'page_size'             => 'filled|numeric',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }
        $params = $request->input();
        $params['page_size'] = 10000;
        $params['page'] = 1;
        $listData = app('repo_user_withdraw')->getCorporateWithDrawQueryList($params);
        $list = $listData['lists'];
        $status_arr = [1 => "待审核", 2 => "通过", 3 => "拒绝"];

        if ($list) {
            $result = [];
            foreach ($list as $item) {
                $temp = [
                    $item->id,
                    $item->user_code,
                    $item->nickname,
                    $item->cardholder,
                    $item->bank_zhi,
                    $item->bank_num,
                    $item->mobile,
                    $item->gold,
                    $item->money_fee,
                    $item->money,
                    $item->add_time,
                    $status_arr[$item->platform_status],
                ];
                $result[] = $temp;
            }

            $headings = [
                "序号",
                "用户编号",
                "用户昵称",
                "公司名称",
                "开户行",
                "银行卡号",
                "银行预留联系方式",
                "提现金币",
                "手续费",
                "税后金额",
                "提现时间",
                "运营审核状态",
            ];

            return app('repo_export')->export(33, $params, $headings, $result);
        }
    }

}
